﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class BaseData_AllPropertyEdit : System.Web.UI.Page
{
    protected void Page_PreInit(object sender, EventArgs e)
    {
        Page.Theme = BLL.Common.GetWSMThemes();
    }

    private string Code
    {
        get
        {
            if (Request.QueryString["code"] != null)
            {
                return Request.QueryString["code"].ToString();
            }
            else
            {
                return "";
            }
        }
    }

    private Model.SHUXING Property
    {
        get
        {
            Model.SHUXING property = null;
            if (ViewState["Property"] != null)
            {
                property = (Model.SHUXING)ViewState["Property"];
            }
            return property;
        }
        set
        {
            ViewState["Property"] = value;
        }
    }
    private bool IsAdd
    {
        get
        {
            return Convert.ToBoolean(ViewState["IsAdd"]);
        }
        set
        {
            ViewState["IsAdd"] = value;
        }

    }
    protected void Page_Load(object sender, EventArgs e)
    {
        //Check.CheckPermission("200300104");
        if (!IsPostBack)
        {            
            if (this.Code != "")
            {
                Label1.Text = "编辑属性";
                this.Property = new BLL.SHUXING().GetModel(int.Parse(this.Code));
                if (this.Property != null)
                {
                    txtName.Text = Property.SX_MC;
                    DD_FJSX.SelectedValue = Property.SX_TABLE;
                    CKB_effect.Checked = (Property.ISDEL == 1 ? false : true);
                    txtRemark.Text = Property.REMARK;
                    this.IsAdd = false;
                    btnAddOrUpdate.Text = "更新";
                }
                else
                {
                    Page.RegisterStartupScript("StartUp", "<script language=javascript>alert('系统中无此记录!');location.href='AllProperty.aspx';</script>");
                }
            }
            else
            {
                this.IsAdd = true;
                btnAddOrUpdate.Text = "添加";
            }
            btnCancel.Attributes.Add("onclick", "if(!confirm('要取消" + btnAddOrUpdate.Text + "并返回上一页吗?'))return false;");
        }
    }
    protected void btnAddOrUpdate_Click(object sender, EventArgs e)
    {
        if (this.IsAdd == true)
        {
            if (true)//Check.CheckPermission("200300104", "00001")
            {
                if (txtName.Text.Length<1)
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('属性名不能为空')", true);
                    return;
                }
                if (DD_FJSX.SelectedIndex<1)
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('请选择对应属性表')", true);
                    return;
                }
                Model.SHUXING property = new Model.SHUXING();
                property.SX_MC = txtName.Text;
                property.SX_TABLE = DD_FJSX.SelectedValue;
                property.ISDEL = CKB_effect.Checked == true ? 0 : 1;
                property.REMARK = txtRemark.Text;
                if (new BLL.SHUXING().Add(property) > 0)
                {
                    //Share.InsertLog("200300104", "00001");
                    Page.RegisterStartupScript("StartUp", "<script language=javascript>alert('添加记录成功');location.href='AllProperty.aspx';</script>");
                }
                else
                {
                    Share.Alert(this.Page, "添加记录失败,请重新添加。");
                }

            }
            else
            {
                Page.RegisterStartupScript("StartUp", "<script language=javascript>alert('无权进行此操作');location.href='AllProperty.aspx';</script>");
            }
        }
        else
        {
            if (true)//Check.CheckPermission("200300104", "00002")
            {
                if (txtName.Text.Length<1)
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('属性名不能为空')", true);
                    return;
                }
                if (DD_FJSX.SelectedIndex<1)
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('请选择对应属性表')", true);
                    return;
                }
                Property.SX_ID = int.Parse(Code);
                Property.SX_MC = txtName.Text;
                Property.SX_TABLE = DD_FJSX.SelectedValue;
                Property.ISDEL = CKB_effect.Checked == true ? 0 : 1;
                Property.REMARK = txtRemark.Text;
                if (new BLL.SHUXING().Update(this.Property))
                {
                    //Share.InsertLog("200300104", "00002");
                    Page.RegisterStartupScript("StartUp", "<script language=javascript>alert('更新记录成功');location.href='AllProperty.aspx';</script>");
                }
                else
                {
                    Share.Alert(this.Page, "更新记录失败,请重新编辑更新。");
                }
            }
            else
            {
                Page.RegisterStartupScript("StartUp", "<script language=javascript>alert('无权进行此操作');location.href='AllProperty.aspx';</script>");
            }

        }
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("AllProperty.aspx");
    }
    
}
